#ifndef _HPCR_MEMORY_H_
#define _HPCR_MEMORY_H_

#include "hpcr_types.h"
#include "core/hpcr_list.h"
#include "core/hpcr_map.h"

// 共享内存的抽象描述
struct hpcr_memory_pool
{
    hpcr_list_t node;
    hpcr_list_t use;
    hpcr_list_t free;
    hpcr_size_t size;
    void *buffer;
};
typedef struct hpcr_memory_pool hpcr_memory_pool_t;

// 申请的每块Memory前端都要用这个hpcr_memory_header_t标识
struct hpcr_memory_header
{
    hpcr_list_node_t item;
    hpcr_size_t size;
};
typedef struct hpcr_memory_header hpcr_memory_header_t;

// 获取MemoryHeader Size的宏定义
#define MemorySize(x) *(hpcr_size_t *)((hpcr_uint8_t *)x + offset(hpcr_memory_header_t, size))
#define MemorySpace(x) (void *)((hpcr_uint8_t *)x + sizeof(hpcr_memory_header_t))
// 从内存指针中获取空间
#define MemoryItem(x) (void *)((hpcr_uint8_t *)x - sizeof(hpcr_memory_header_t))

// 下面定义一些共享内存的操作
void hpcr_memory_pool_init(hpcr_memory_pool_t *mp, void *buffer, hpcr_size_t size);
void *hpcr_memory_alloc(hpcr_memory_pool_t *mp, hpcr_size_t size);
bool hpcr_memory_free(hpcr_memory_pool_t *mp, void *ptr);

#endif
